package com.example.personalcenter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

/**
 * 我的Fragment - 显示用户信息、兴趣，提供选择兴趣和退出登录功能
 */
public class ProfileFragment extends Fragment {
    
    private static final int REQUEST_CODE_INTEREST = 100;
    
    private TextView tvUsername;
    private TextView tvInterest;
    private Button btnSelectInterest;
    private Button btnLogout;
    private SharedPreferences sharedPreferences;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        
        // 初始化控件
        tvUsername = view.findViewById(R.id.tv_username);
        tvInterest = view.findViewById(R.id.tv_interest);
        btnSelectInterest = view.findViewById(R.id.btn_select_interest);
        btnLogout = view.findViewById(R.id.btn_logout);
        
        // 获取SharedPreferences
        sharedPreferences = requireActivity().getSharedPreferences("PersonalCenter", Context.MODE_PRIVATE);
        
        // 加载用户数据
        loadUserData();
        
        // 选择兴趣按钮点击事件
        btnSelectInterest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireActivity(), InterestSelectActivity.class);
                startActivityForResult(intent, REQUEST_CODE_INTEREST);
            }
        });
        
        // 退出登录按钮点击事件
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutDialog();
            }
        });
        
        return view;
    }
    
    /**
     * 加载用户数据
     */
    private void loadUserData() {
        String username = sharedPreferences.getString("username", "未登录");
        String interest = sharedPreferences.getString("interest", "未选择");
        
        tvUsername.setText("邮箱：" + username);
        tvInterest.setText("当前兴趣：" + interest);
    }
    
    /**
     * 接收兴趣选择结果
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_INTEREST && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                String interest = data.getStringExtra("interest");
                if (interest != null && !interest.isEmpty()) {
                    // 保存兴趣到SharedPreferences
                    sharedPreferences.edit().putString("interest", interest).apply();
                    
                    // 更新UI
                    tvInterest.setText("当前兴趣：" + interest);
                    
                    // 通知HomeActivity刷新首页Fragment
                    if (getActivity() instanceof HomeActivity) {
                        ((HomeActivity) getActivity()).refreshHomeFragment();
                    }
                }
            }
        }
    }
    
    /**
     * 显示退出登录确认对话框
     */
    private void showLogoutDialog() {
        new AlertDialog.Builder(requireActivity())
                .setTitle("确认退出")
                .setMessage("是否确定要退出登录？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 清除SharedPreferences中的用户数据
                        sharedPreferences.edit()
                                .remove("username")
                                .remove("interest")
                                .apply();
                        
                        // 返回登录页面
                        Intent intent = new Intent(requireActivity(), MainActivity.class);
                        startActivity(intent);
                        requireActivity().finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }
}
